package com.swift.maintenance.base;

import android.app.ActivityManager;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.baidu.mapapi.SDKInitializer;
import com.jiyi.easemob.util.EaseCacheUtil;
import com.jiyi.easemob.util.EaseMobHelp;
import com.lzy.okgo.OkGo;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.swift.base.ApplicationBase;
import com.swift.maintenance.common.WebApi;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.utils.SPUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.Iterator;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

public class AppBase extends ApplicationBase {
	final String TAG = AppBase.class.getName();
	private static int mScreenWidth;
	private static int mScreenHeight;
	public static WebApi WebClient;

	@Override
	protected void attachBaseContext(android.content.Context base) {
		super.attachBaseContext(base);
		MultiDex.install(this) ;
	}

	@Override
	public void onCreate() {
		// 判断如果是服务进程则不需要重新初始化
		int pid = android.os.Process.myPid();
		String processAppName = getAppName(pid);

		Log.e("CloudLifeApplication ", "process app name : " + processAppName);

		if (processAppName == null
				|| !processAppName.equalsIgnoreCase(getPackageName())) {
			Log.e("CloudLifeApplication ", "enter the service process!");
			return;
		}
//
		super.onCreate();
		MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
		WebClient = new WebApi();
		WebClient.setremoteHost(AppConfig.TEXT_HOST);
		ToastUtil.getInstances().init(this);
//		// 百度地图 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		SDKInitializer.initialize(getApplicationContext());
		SPUtils.getInstance().inits(this);
//		//环信用户缓存初始化
		EaseCacheUtil.getInstance().init();
//		//环信初始化
		EaseMobHelp.getInstance().init(this);
		//极光推送
//		JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
//		// initScreenWH();

		OkGo.getInstance().init(this);

		/***
		 * 初始化logger
		 */
		FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
				.methodCount(1)         // (Optional) How many method line to show. Default 2
				.tag("weizesong")
				.build();
		Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
	}

	private void initScreenWH() {
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		boolean isPortrait = dm.widthPixels < dm.heightPixels;
		mScreenWidth = isPortrait ? dm.widthPixels : dm.heightPixels;
		mScreenHeight = isPortrait ? dm.heightPixels : dm.widthPixels;
		Log.d(TAG, " mScreenWidth " + mScreenWidth);
		Log.d(TAG, " mScreenHeight " + mScreenHeight);
		float densityvalue = getResources().getDisplayMetrics().density;
		int densitydpi = getResources().getDisplayMetrics().densityDpi;
		Log.d(TAG, " density " + getResources().getDisplayMetrics().densityDpi);
		// density 2 dpi 320 GENY 768P xhdpi
		// density 3 dpi 480 GENY 1080P xxhdpi
		// density 1.875 dpi 300 夜神 1080P xhdpi
		// density 1.5625 dpi 250 夜神 720P hdpi
		// density 1 dpi dpi 160 夜神 400P mdpi

	}

	private String getAppName(int pID) {
		String processName = null;
		ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		PackageManager pm = getPackageManager();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i
					.next());
			try {
				if (info.pid == pID) {
					CharSequence c = pm.getApplicationLabel(pm
							.getApplicationInfo(info.processName,
									PackageManager.GET_META_DATA));
					// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
					// info.processName +"  Label: "+c.toString());
					// processName = c.toString();
					processName = info.processName;
					return processName;
				}
			} catch (Exception e) {
				// Log.d("Process", "Error>> :"+ e.toString());
			}
		}
		return processName;
	}
}
